package com.cxs.plan.config;

import com.alibaba.fastjson.JSON;
import com.cxs.plan.constant.HttpStatus;
import com.cxs.plan.domain.HttpResult;
import com.cxs.plan.utils.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token检验失败自定义处理
 */
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        HttpResult result = new HttpResult(HttpStatus.UNAUTHORIZED, "未认证或已过期", null);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response,json);

    }
}
